#!/usr/bin/env python
# coding: utf-8

import re
import gzip
import sys
from itertools import islice

GO_TYPES = ['P', 'F', 'C']

def parse_go_funcs(s):
    d = {}
    for tf in s.split():
        t,f = tf.split(':')
        assert t not in d
        d[t] = frozenset(f.split(','))
    return d

def is_entry_equal(s1, s2):
    assert s1 or s2
    if s1.difference(s2) and s2.difference(s1):
        return False
    return True


def main():
    compare_type = sys.argv[1].strip()
    assert compare_type in ['exact', 'entry']

    real_funcs_dict = dict((t, set()) for t in GO_TYPES)
    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue
        for t,f in parse_go_funcs(line).items():
            real_funcs_dict[t].add(f)

    pw_size = max(len(f) for f in real_funcs_dict.itervalues())
    results = dict((t, [(0, len(real_funcs_dict[t]))]) for t in GO_TYPES)

    line_id = 0
    for line in gzip.open(sys.argv[2]):
        line = line.strip()
        if not line:
            continue
        line_id += 1
        pred_func = line.split('\t')[1].strip()
        pfd = parse_go_funcs(pred_func)
        for t,pf in pfd.items():
            if compare_type == 'exact':
                if pf in real_funcs_dict[t]:
                    real_funcs_dict[t].discard(pf)
                    results[t].append((line_id, len(real_funcs_dict[t])))
            elif compare_type == 'entry':
                for rf in real_funcs_dict[t]:
                    if is_entry_equal(rf, pf):                    
                        real_funcs_dict[t].discard(rf)
                        results[t].append((line_id, len(real_funcs_dict[t])))
                        break

        if sum(len(real_funcs_dict[t]) for t in real_funcs_dict) == 0 or line_id > max(500, pw_size):
            break

    return '|'.join('%s %s' % (t, ' '.join('%d:%d' % (i,n) for i,n in results[t])) for t in GO_TYPES)

print main()

